﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Framework.Core.Extension
{
    public static class StringExtension
    {
        #region 获取指定长度的随机字符串
        /// <summary>
        /// 获取指定长度的随机字符串
        /// </summary>
        /// <param name="length">生成的字符串长度</param>
        /// <param name="bol">是否有复杂字符</param>
        /// <returns></returns>
        public static string GetRandomString(int length, bool bol = false)
        {
            string str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            if (bol)
            {
                str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";//复杂字符
            }
            StringBuilder sb = new StringBuilder();
            Random rd = new Random();
            for (int i = 0; i < length; i++)
            {
                sb.Append(str.Substring(rd.Next(0, str.Length), 1));
            }
            return sb.ToString();
        }
        #endregion

        #region 大小写,下划线转换
        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToPascalCase(this string s)
        {
            if (string.IsNullOrWhiteSpace(s))
            {
                return s;
            }

            var builder = new StringBuilder();
            bool wordBreaked = true;
            foreach (char c in s)
            {
                if (char.IsWhiteSpace(c) || c == '.' || c == '-' || c == '_')
                {
                    wordBreaked = true;
                    continue;
                }

                if (wordBreaked)
                {
                    builder.Append(char.ToUpper(c));
                    wordBreaked = false;
                }
                else if (char.IsUpper(c))
                {
                    builder.Append(c);
                }
                else
                {
                    builder.Append(char.ToLower(c));
                }
            }

            return builder.ToString();
        }

        public static string ToCamelCase(this string s)
        {
            if (string.IsNullOrWhiteSpace(s))
            {
                return s;
            }
            return char.ToLower(s[0]).ToString() + s.ToPascalCase().Remove(0, 1);
        }

        public static string ToSnakeCase(this string s)
        {
            if (string.IsNullOrWhiteSpace(s))
            {
                return s;
            }
            string replacement = s.ToPascalCase();
            var builder = new StringBuilder();
            foreach (char c in replacement)
            {
                if (char.IsUpper(c))
                {
                    builder.Append('_');
                }
                builder.Append(char.ToLower(c));
            }
            return builder.ToString().TrimStart('_');
        }

        /// <summary>
        /// 该方法适用于 对象被转成了Json字符串,这个时候需要将属性名称格式化为 Snake格式, 属性值保持不变
        /// Tip: 属性值不能包含逗号,否则会出错        
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToSnakeCaseForObject(this string s)
        {
            if (string.IsNullOrWhiteSpace(s))
            {
                return s;
            }

            StringBuilder sb = new StringBuilder();

            //将字符串按逗号分隔
            string[] strPros = s.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            if (strPros.Length <= 0)
            {
                return s;
            }
            foreach (string pro in strPros)
            {
                string[] pros = pro.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);

                sb.Append(SnakeCaseSub(pros[0]));

                for (int i = 1; i < pros.Length; i++)
                {
                    if (!pros[i].Contains("{\""))
                    {
                        sb.Append($":{pros[i]}");
                    }
                    else
                    {
                        sb.Append($":{SnakeCaseSub(pros[i])}");
                    }
                }
                sb.Append(",");
            }

            return sb.ToString().TrimEnd(',');
        }

        private static string SnakeCaseSub(string pro)
        {
            string sn = pro.ToSnakeCase();
            int snIndex = sn.IndexOf("_");
            return $"{sn.Substring(0, snIndex)}{sn.Substring(snIndex + 1, sn.Length - snIndex - 1)}";
        }
        #endregion

        #region 获取字符串中大写字母的下标
        public static List<int> GetCapitalIndex(this string s)
        {
            List<int> result = new List<int>();
            var sArray = s.ToCharArray();
            for (int i = 0; i < sArray.Length; i++)
            {
                if(sArray[i]>='A'&&sArray[i]<='Z')
                {
                    result.Add(i);
                }
            }
            return result;
        }
        #endregion
    }
}
